package com.liao.ticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liao.ticket.common.exception.ErrorCode;
import com.liao.ticket.dao.dos.ViewManager;
import com.liao.ticket.dao.mapper.UserMapper;
import com.liao.ticket.dao.pojo.User;
import com.liao.ticket.service.LoginService;
import com.liao.ticket.service.UserService;
import com.liao.ticket.util.PassUtils;
import com.liao.ticket.util.UserThreadLocal;
import com.liao.ticket.vo.Result;
import com.liao.ticket.vo.UserVo;
import com.liao.ticket.vo.params.LoginParams;
import com.liao.ticket.vo.params.UserParams;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LoginService loginService;

    @Override
    public User findUserByAccountAndPass(String account, String password) {
        if (StringUtils.isBlank(account) || StringUtils.isBlank(password)) {
            return null;
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getPhone, account).eq(User::getPassword, password);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public User findUserByPhone(String phone) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getPhone, phone);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    @Override
    public void save(User user) {
        userMapper.insert(user);
    }

    @Override
    public UserVo userVoSelfInfo() {
        User user = UserThreadLocal.get();
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        return userVo;
    }

    @Override
    public Result updateSelfInfo(UserParams userParams) {
        String phone = userParams.getPhone();
        String idcard = userParams.getIdcard();
        String name = userParams.getName();
        if(!StringUtils.isBlank(phone)){
            if(selectByPhone(phone) != null){
                return Result.fail(ErrorCode.USER_UPDATE_PHONE_FAIL);
            }
        }
        if((StringUtils.isBlank(idcard)
        && StringUtils.isBlank(name)
        && StringUtils.isBlank(phone)) || phone.length() > 11){
            return Result.fail(ErrorCode.USER_UPDATE_PARAMS_FAIL);
        }
        User user = new User();
        user.setId(UserThreadLocal.get().getId());
        user.setName(user.getName());
        user.setPhone(phone);
        user.setIdcard(idcard);
        return Result.success(userMapper.updateById(user));
    }

    @Override
    public Result updateSelfPass(LoginParams loginParams) {
        if (StringUtils.isBlank(loginParams.getPassword())) {
            return Result.fail(ErrorCode.USER_PARAMS_FAIL);
        }
        User user = new User();
        user.setId(UserThreadLocal.get().getId());
        user.setPassword(PassUtils.thicken(loginParams.getPassword()));
        return Result.success(userMapper.updateById(user));
    }

    @Override
    public Result deleteSelfAccount(String token) {
        int i = userMapper.deleteById(UserThreadLocal.get().getId());
        loginService.logout(token);
        return Result.success(i);
    }

    @Override
    public Integer updateMileageAndConsumeById(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public User selectById(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    public ViewManager selectManagerByIdAndType(Long id, Integer type) {
        return userMapper.selectManagerByUidAndType(id, type);
    }

    private User selectByPhone(String phone) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getPhone, phone);
        return userMapper.selectOne(wrapper);
    }
}
